import axios from 'axios';

const API_URL = import.meta.env.VITE_API_URL;

/**
 * 文案处理相关API服务
 */
const copywritingService = {
    /**
     * 处理文件夹中的视频提取文本
     * @param {string} folderPath - 视频文件夹路径
     * @param {string} apiKey - OpenAI API密钥
     * @returns {Promise} - 处理结果
     */
    processFolder(folderPath, apiKey) {
        return axios.post(`${API_URL}/copywriting/process_folder`, {
            folder_path: folderPath,
            api_key: apiKey
        })
            .then(response => response.data)
            .catch(error => {
                console.error('处理文件夹失败:', error);
                throw error;
            });
    },

    /**
     * 获取默认提示词
     * @returns {Promise} - 默认提示词
     */
    getDefaultPrompt() {
        return axios.get(`${API_URL}/copywriting/default_prompt`)
            .then(response => response.data)
            .catch(error => {
                console.error('获取默认提示词失败:', error);
                throw error;
            });
    },

    /**
     * 生成单个文案
     * @param {string} transcript - 文本内容
     * @param {string} apiKey - OpenAI API密钥
     * @param {string} customPrompt - 自定义提示词
     * @returns {Promise} - 生成的文案
     */
    generateCopywriting(transcript, apiKey, customPrompt) {
        return axios.post(`${API_URL}/copywriting/generate`, {
            transcript,
            api_key: apiKey,
            custom_prompt: customPrompt
        })
            .then(response => response.data)
            .catch(error => {
                console.error('生成文案失败:', error);
                throw error;
            });
    },

    /**
     * 批量生成文案
     * @param {Array} transcripts - 文本数组
     * @param {string} apiKey - OpenAI API密钥
     * @param {string} customPrompt - 自定义提示词
     * @returns {Promise} - 批量生成结果
     */
    // 批量生成文案
    generateBatch(transcripts, apiKey, customPrompt) {
        return axios.post(`${API_URL}/copywriting/generate_batch`, {
            transcripts,
            api_key: apiKey,
            custom_prompt: customPrompt
        })
            .then(response => response.data)
            .catch(error => {
                console.error('批量生成文案失败:', error);
                throw error;
            });
    },

    /**
     * 导出文案到Excel
     * @param {Array} data - 文案数据数组
     * @returns {Promise} - 导出结果
     */
    exportToExcel(data) {
        return axios.post(`${API_URL}/copywriting/export_excel`, {
            data
        }, {
            responseType: 'blob'
        })
            .then(response => {
                // 创建Blob对象
                const blob = new Blob([response.data], {
                    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                });

                // 创建下载链接
                const url = window.URL.createObjectURL(blob);
                const link = document.createElement('a');
                link.href = url;
                link.setAttribute('download', `文案导出_${new Date().toISOString().slice(0, 10)}.xlsx`);
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);

                return { success: true, message: '导出成功' };
            })
            .catch(error => {
                console.error('导出Excel失败:', error);
                throw error;
            });
    }
};

export default copywritingService;